package com.sy.rabbitmq.dlx;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.sy.rabbitmq.TestProperties;

import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.TimeoutException;

/**
 * dlx-死信队列-生成端
 */
public class Procuder {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory = new ConnectionFactory();
        factory.setUsername(TestProperties.getUsername());
        factory.setPassword(TestProperties.getPassword());
        factory.setHost(TestProperties.getIp());
        factory.setPort(TestProperties.getPort());
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.confirmSelect();
        String exchangeName = "test_dlx_exchange";
        String routeKey = "dlx.save";
        //发送一条消息
        String msg = "Hello RabbitMQ DLX message!";
        int i = 0;
        while(i<5){
            //设置头
            HashMap<String, Object> headers = new HashMap<String, Object>();
            headers.put("num",i);
            AMQP.BasicProperties properties = new AMQP.BasicProperties().builder()
                    .deliveryMode(2)
                    .contentEncoding("UTF-8")
                    //todo 当消息超时也会进入死信队列中
                    .expiration("10000")
                    .headers(headers)
                    .build();

            //发送消息
            channel.basicPublish(exchangeName,routeKey,true,properties,msg.getBytes());
            i++;
            System.out.println(i);
        }
        channel.close();
    }
}
